const webpack = require("webpack");
const path = require("path");
const { VueLoaderPlugin } = require("vue-loader");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const packageConfigs = require("./package.json");

module.exports = {
  entry: "./src/main.js",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "[name].[chunkhash:8].js",
    publicPath: packageConfigs.homepage || "/",
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        use: "vue-loader",
      },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: "babel-loader",
      },
      {
        test: /\.less$/i,
        use: ["style-loader", "css-loader", "less-loader"],
      },
      {
        test: /\.css$/i,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: "url-loader",
            loader: "file-loader",
            options: {
              name: "[name].[contenthash:8].[ext]",
              limit: 10240,
              esModule: false, // 不设置导出的是一个ES Module，设置后直接返回url
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new VueLoaderPlugin(),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "./public/index.html"),
      title: "part2-2 作业",
    }),
    new webpack.DefinePlugin({
      // 从package.json文件中读取homepage，如果发布到 pages.com/mypage/下的时候
      BASE_URL: JSON.stringify(packageConfigs.homepage || ""),
    }),
  ],
};
